﻿#include "heap.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
    struct heap hp, *ptr;
    struct heap_node *node;
    int i = 0;

    // 堆初始化
    heap_init(&hp, MIN_HEAP);

    ptr = &hp;

    // 插入数据
    heap_insert(ptr, 0, NULL);
    heap_insert(ptr, 2, NULL);
    heap_insert(ptr, 1, NULL);
    heap_insert(ptr, 5, NULL);
    heap_insert(ptr, 3, NULL);
    heap_insert(ptr, 23, NULL);
    heap_insert(ptr, 24, NULL);
    heap_insert(ptr, 15, NULL);
    heap_insert(ptr, 8, NULL);
    heap_insert(ptr, 26, NULL);
    heap_insert(ptr, 35, NULL);
    heap_insert(ptr, 31, NULL);
    heap_insert(ptr, 17, NULL);
    heap_insert(ptr, 16, NULL);
    heap_insert(ptr, 9, NULL);
    heap_insert(ptr, 45, NULL);
    heap_insert(ptr, 65, NULL);
    heap_insert(ptr, 27, NULL);
    heap_insert(ptr, 14, NULL);
    heap_insert(ptr, 52, NULL);
    heap_insert(ptr, 38, NULL);
    heap_insert(ptr, 44, NULL);
    heap_insert(ptr, 4, NULL);
    heap_insert(ptr, 7, NULL);

    // 删除数据
    heap_delete(ptr);
    heap_delete(ptr);
    heap_delete(ptr);

    // 查找
    node = heap_find(ptr, 5);
    if(NULL != node)
        printf("key %d found(@0x%p)\n", 5, node->data);
    else
        printf("key 5 not found.\n");
    node = heap_find(ptr, 19);
    if(NULL != node)
        printf("key %d found(@0x%p)\n", 19, node->data);
    else
        printf("key 19 not found.\n");

    // 排序
    heap_sort(ptr);

    printf("heap sort:\n");
    for(i = 0; i < ptr->size; i++)
        printf("%d ", ptr->nodes[i]);
    return 0;
}